package org.budo.redis.repository.jedis.graph;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author lmw
 */
@Slf4j
public class BudoRedisGraphServiceImpl implements BudoRedisGraphService {
    public static BudoRedisGraphService INSTANCE = null;

    public BudoRedisGraphServiceImpl() {
        INSTANCE = this;
    }

    @Override
    public String get(JedisPool jedisPool, String key) {
        Jedis jedis = jedisPool.getResource();

        try {
            return jedis.get(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public byte[] get_by_bytes(JedisPool jedisPool, byte[] key) {
        Jedis jedis = jedisPool.getResource();

        try {
            return jedis.get(key);
        } finally {
            this.closeJedis(jedis);
        }
    }

    @Override
    public String set(JedisPool jedisPool, String key, String value) {
        Jedis jedis = jedisPool.getResource();

        try {
            return jedis.set(key, value);
        } finally {
            this.closeJedis(jedis);
        }
    }

    private void closeJedis(Jedis jedis) {
        if (null == jedis) {
            return;
        }

        try {
            jedis.close();
        } catch (Throwable e) {
            log.error("#37 closeJedis error, e=" + e + ", jedis=" + jedis, e);
        }
    }
}